-
Notifications
You must be signed in to change notification settings - Fork 38
Add source names (via new Stream
and SourceSpan
classes) and .span()
combinator (take 2)
#85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add source names (via new Stream
and SourceSpan
classes) and .span()
combinator (take 2)
#85
Conversation
For backwards compatibility, these are only used to wrap input data when a source is given to `parse()` or `parse_partial()`. When a source is given, the following behaviours change: - the primitive `line_info` parser returns a 3-tuple instead of a 2-tuple - ParseError objects will include the source
be70346
to
5c5b293
Compare
Hi @spookylukey, I need your help getting this merged -- can you let me know when you'll be able to check out the PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks really good, thanks for the PR.
In addition to the comment above, please could you also:
- add a test that covers
make_stream()
being passed bytes. Currently if I comment out or break those lines, no test fails. - for completeness, a test covering the exception in
make_stream()
as well. - add an entry into history.rst, mentioning all the additions,
plus the change in behaviour forline_info_at()
if the newsource
parameters is passed.
Thanks!
Luke
def make_stream(data: str | bytes, source: Any): | ||
"""Constructs an appropriate stream type for `data` when it's one of the | ||
three core supported datatypes of parsy (viz. str, bytes, list). Otherwise, | ||
the data is assumed to just support a minimum of __getitem__ and | ||
__len__.""" | ||
if isinstance(data, str): | ||
return StrStream(data, source) | ||
|
||
if isinstance(data, bytes): | ||
return ByteStream(data, source) | ||
|
||
raise RuntimeError( | ||
"A Parsy stream can be formed only on str and bytes, but the given " | ||
f"data has type {type(data)}. If you are separately tokenizing the " | ||
"data to parse, consider instead equipping the tokens with source " | ||
"location metadata.", | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't look like it handles list
, in contrast to its docstring. That's OK, but the docstring should be corrected, and preferably the limitation should be noted in the appropriate docs i.e. the parse method.
Following up from #83, here's a new PR:
str
andbytes
subclasses equipped with asource
attribute, wrapping the user's input only when asource
is provided to.parse()
or.parse_partial()
.The test suite is virtually unchanged this time around. I took care to keep everything as backwards compatible as possible, including keeping
line_info
returning a 2-tuple when no source is given.